package com.ttai.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerUapComponet;
import com.ttai.dagger.module.activity.UapPresenterModule;
import com.ttai.model.dao.bean.Acount_Item;
import com.ttai.presenter.activity.InsertNewAcountPresenter;
import com.ttai.presenter.activity.InsertTipAcountPresenter;
import com.ttai.presenter.activity.SetKeyTipPresenter;
import com.ttai.presenter.activity.UapPresenter;
import com.ttai.ui.adapter.UapRecycleViewAdapter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.Constant;
import com.ttai.untils.ItemClickSupport;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UaP_Protect extends BaseActivity {
    @Bind(R.id.ib_addacount)
    Button ibAddacount;
    @Bind(R.id.rv_acount)
    RecyclerView rvAcount;

    @Inject
    UapPresenter uapPresenter;
    InsertNewAcountPresenter insertNewAcountPresenter;
    InsertTipAcountPresenter insertTipAcountPresenter;
    SetKeyTipPresenter setKeyTipPresenter;


    public UapRecycleViewAdapter adapter;
    @Bind(R.id.resetKey)
    Button resetKey;
    private String appName;
    private String value;
    public ArrayList<Acount_Item> acountList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uap_protect);
        ButterKnife.bind(this);
        setComponet();
        insertNewAcountPresenter = new InsertNewAcountPresenter(this);
        insertTipAcountPresenter = new InsertTipAcountPresenter();
        insertTipAcountPresenter.storeTipAcount(Constant.getToken());

//        设置安全缩略图
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

//设置adapter
        adapter = new UapRecycleViewAdapter(this, this);
        rvAcount.setAdapter(adapter);
        rvAcount.setLayoutManager(new GridLayoutManager(this, 3));

//        uapPresenter.getData();
        insertNewAcountPresenter.storeNewAcount(Constant.getToken());

        ItemClickSupport.addTo(rvAcount).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, final int position, View v) {
                final List<Acount_Item> list = insertNewAcountPresenter.getList();
                Acount_Item acount_item = list.get(position);
                appName = acount_item.getAcount_name();
                AlertDialog.Builder builder = new AlertDialog.Builder(UaP_Protect.this);
                builder.setMessage("确定删除?");
                builder.setTitle("提示");

                //添加AlertDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (list.remove(position) != null) {
                            acountList = insertNewAcountPresenter.getList();
                            String token = Constant.getToken();
                            uapPresenter.acountDelete(token, appName);

                        } else {
//                            System.out.println("failed");
                        }
//                        acountAdapter.notifyDataSetChanged();
//                        Toast.makeText(getBaseContext(), "删除成功", Toast.LENGTH_SHORT).show();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.create().show();
                return true;
            }
        });
        ItemClickSupport.addTo(rvAcount).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                Log.i("45", "onItemClicked: 55");
                final List<Acount_Item> list = insertNewAcountPresenter.getList();
                Acount_Item acount_item = list.get(position);
                String acountName = acount_item.getAcount_name();
                String imgUrl = acount_item.getImgUrl();
                Intent intent = new Intent(UaP_Protect.this, AcountItem.class);
                intent.putExtra("aname", acountName);
                intent.putExtra("img",imgUrl);
//                intent.putExtra("aphone", UapPresenter.phonenumber);
                value = insertTipAcountPresenter.getTip();
                Log.i("444", "onCreate: tip:" + value);
                intent.putExtra("value", value);
                startActivity(intent);
                Toast.makeText(UaP_Protect.this, acountName, Toast.LENGTH_SHORT).show();
            }
        });

    }

    public UapRecycleViewAdapter getAdapter() {
        return adapter;
    }


    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void setComponet() {
        DaggerUapComponet.builder()
                .uapPresenterModule(new UapPresenterModule(this))
                .build()
                .in(this);
    }


    public void deleteTip() {
        setKeyTipPresenter = new SetKeyTipPresenter(this);
        setKeyTipPresenter.setKetTip(Constant.getToken(), "");
    }

    @OnClick({R.id.ib_addacount, R.id.resetKey})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_addacount:
                Intent intent1 = new Intent();
                intent1.setClass(UaP_Protect.this, AddAcount.class);
                startActivity(intent1);
                break;
            case R.id.resetKey:
                if (insertNewAcountPresenter.getList().size()>0){
                    Intent intent2 = new Intent();
                    intent2.setClass(UaP_Protect.this, ResetKey.class);
                    startActivity(intent2);
                }else {
                    Toast.makeText(UaP_Protect.this,"账号密码列表为空,无法修改",Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }
}
